package com.example.administrator.bdsjws.utils;

import android.app.NotificationManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Environment;
import android.support.v7.app.NotificationCompat;


import com.example.administrator.bdsjws.MyApplication;
import com.example.administrator.bdsjws.R;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;


//普通JAVA类，是没有上下文
public class MyUtil {
    //返回共享首选项对象
    public static SharedPreferences getSharedPre(){
         //获取上下文  再获取共享首选项
        return  MyApplication.getContext().getSharedPreferences("loginnew", Context.MODE_PRIVATE);


    }
    public static String gethttp(String path) throws IOException {
        String json = null;
        URL url=new URL(path);
        HttpURLConnection conn= (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        InputStream im=conn.getInputStream();
        BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(im));
        json=bufferedReader.readLine();
        im.close();
        return json;
    }

    public static boolean isSDCard(){
        String state = Environment.getExternalStorageState();
        if(Environment.MEDIA_MOUNTED.equals(state)){
            return true;
        }else{
            return false;
        }
    }

    //能否读写 能写就写入SD卡，不能就写手机存储中
    public static File getPath(){
        File path = null;
        if(isSDCard()){
            path = Environment.getExternalStorageDirectory();
        }else{
            path = Environment.getDownloadCacheDirectory();
        }

        return path;
    }

    public static void createNotification(){
        NotificationManager notificationManager = (NotificationManager) MyApplication.getContext().getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationCompat.Builder    notificationBuild = new NotificationCompat.Builder(MyApplication.getContext());
        notificationBuild.setContentTitle("下载中……")
                .setContentText("准备下载……")
                .setSmallIcon(R.mipmap.logo_small)
                .setOngoing(true) //设置他为一个正在进行的通知
                .setAutoCancel(false);
        notificationManager.notify(0, notificationBuild.build());

    }

    public static boolean isSD(){
        boolean b=Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
        if (b){
            return true;
        }else{
            return false;
        }
    }


}
